package cn.edu.sctu.sctu_lysd.view.fra;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.adapter.MyAcceptOrderAda;
import cn.edu.sctu.sctu_lysd.adapter.MyReleaseOrderAda;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseListBean;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.my_view.XListView;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-08.
 */

public class MyOrdersRightFra extends Fragment {

    private View view;


    private MyAcceptOrderAda adapter;
    private LinearLayoutManager mLayoutManager;
    private List<ParcelData> dataList = new ArrayList<>();
    private SharePreferenceUtils sharePreferenceUtils;
    private Call<JsonBaseListBean<ParcelData>> call;
    private NetService service;
    private int nowPage = 1;

    private XListView mXListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_my_orders1, container, false);

        RetrofitHelper helper = RetrofitHelper.getInstance();

        service = (NetService) helper.getService(NetService.class);

        sharePreferenceUtils = SharePreferenceUtils.getInstance(getActivity());

        initView();

        initData();


        return view;
    }

    private void initView() {

        mXListView = (XListView) view.findViewById(R.id.list_view);

        mXListView.setPullRefreshEnable(true);
        mXListView.setPullLoadEnable(true);

        mXListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {

                nowPage = 1;

                initData();
            }

            @Override
            public void onLoadMore() {



                initData();
            }
        });

    }

    private void initData() {

        call = service.getAcceptOrder(sharePreferenceUtils.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN),
                sharePreferenceUtils.getLong(CacheKeyConfig.CACHE_USER_ID), nowPage, 10);

        call.enqueue(new Callback<JsonBaseListBean<ParcelData>>() {
            @Override
            public void onResponse(Call<JsonBaseListBean<ParcelData>> call, Response<JsonBaseListBean<ParcelData>> response) {

                L.e("responsewangfuxin", "response" + response.toString());

                if (response.code() == 200 && response.body().getMeta().getCode() == 200) {

                    dataList = response.body().getData();

                    if (dataList == null || dataList.size() == 0) {//没有更多数据

                        mXListView.stopLoadMore();

                        mXListView.stopRefresh();

                        return;
                    }

                    if (nowPage == 1) {

                        if (adapter == null) {//初次进入页面时

                            adapter = new MyAcceptOrderAda(getContext(), dataList);

                            mXListView.setAdapter(adapter);

                        } else {//刷新时进入

                            mXListView.stopRefresh();

                            adapter.refresh(dataList);

                        }

                    } else {//加载更多

                        mXListView.stopLoadMore();

                        adapter.loadMore(dataList);

                    }


                } else {

                    getDataFailed();
                }

                nowPage++;

            }

            @Override
            public void onFailure(Call<JsonBaseListBean<ParcelData>> call, Throwable t) {

                L.e("responsewangfuxin", "responsewangfuxin");
                L.e("responsewangfuxin", t.getMessage());

            }
        });



    }

    private void getDataFailed() {

        mXListView.stopRefresh();
        mXListView.stopLoadMore();

        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_LONG).show();

    }

}
